import ObjectGetSetUtils from "./ObjectGetSetUtils.js";

const TextTempUtils = {
    /**
     * 解析文字模板数据，解析的easypoi模板，替换里面的变量和解析数字出来
     * @param data 数据
     * @param word 文字模板
     * @returns {[{isNum: boolean, value: string}]}
     */
    pWordReg(data,word) {
        if(!word) {
            return []
        }
        word = (word+'').replace(/\{\{(.*?)}}/g,($0,$1)=>{
            let value = ObjectGetSetUtils.getFieldValue(data,$1.trim());
            if (value === undefined){
                return "";
            }else {
                return value;
            }
        })
        let regStr = '[-+]?\\d+(\\.\\d+)?|[a-zA-Z%]+'
        const reg = new RegExp(regStr, 'g')
        word = word.replace(reg,($0,$1)=>{
            return `<span class="useTImesFont">${$0}</span>`
        })
        let result = word.split('<span class="useTImesFont">').map(i => {
            let r = i.split('</span>')
            let reg1 = new RegExp(regStr)
            let re = [
                {
                    isNum: reg1.test(r[0]),
                    value: r[0],
                }
            ]
            if (r.length === 2) {
                re.push({
                    isNum: reg1.test(r[1]),
                    value: r[1],
                })
            }
            return re
        })
            .flat()
            .filter(i => !!i.value)
        return result
    },
}

export default TextTempUtils